//
//  Double+Extension.swift
//  Library_y
//
//  Created by JC on 2023/6/6.
//

import Foundation
import UIKit

// MARK: - Double常用类型转换
public extension Double {
    
    /// Double 转 Int
    var toInt: Int {
        return Int(self)
    }
    
    /// Double 转 String
    var toString: String {
        return "\(self)"
    }
    
    /// Double 转 CGFloat
    var toCGFloat: CGFloat {
        return CGFloat(self)
    }
    
    /// 准确的小数尾截取 - 没有进位
    func decimal(_ base: Self = 1) -> Double {
        let tempCount: Self = pow(10, base)
        let temp = self*tempCount
        
        let target = Self(Int(temp))
        let stepone = target/tempCount
        if stepone.truncatingRemainder(dividingBy: 1) == 0 {
            return String(format: "%.0f", stepone).toDouble
        }else{
            return "\(stepone)".toDouble
        }
    }
}

